package com.yd.app.service.pc.goodsService;

import com.yd.app.mapper.GoodsMapper;
import com.yd.app.mapper.GoodsWeightMapper;
import com.yd.app.service.common.BaseService;
import com.yd.domain.business.Goods;
import com.yd.domain.business.GoodsPic;
import com.yd.domain.business.Pic;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.GoodsSearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zg
 * @create 2018-03-07 17:44
 * @des 商品
 **/
@Service
public class GoodsService extends BaseService {

    @Autowired
    GoodsMapper goodsMapper;
    @Value("${base.img.url}")
    private String base_img_url;
    @Autowired
    GoodsWeightMapper goodsWeightMapper;
    /**
     * 添加商品基础信息
     * @param goods
     */
    public void addGoods(Goods goods) {
        goodsMapper.addGoods(goods);
    }

    /**
     * 修改商品状态（上/下架）
     * @param goodsId
     */
    public void updateGoodsStatus(String goodsId,String status) {
        goodsMapper.updateGoodsStatus(goodsId,status);
    }

    /**
     * 图片
     * @param goodsId
     */
    public void updateIconUrl(String goodsId,String pic_path) {
        goodsMapper.updateIconUrl(goodsId,pic_path);
    }

    /**
     * 图片
     * @param id
     */
    public void updateIconUrl(String id) {
        goodsMapper.deleteIconUrl(id);
    }

    /**
     * 添加产品轮播图片
     * @param goodsPic
     */
    public void addGoodsPic(GoodsPic goodsPic) {
        goodsMapper.addGoodsPic(goodsPic);
    }

    /**
     * 添加产品轮播图片
     * @param pic
     */
    public void addPic(Pic pic) {
        goodsMapper.addPic(pic);
    }

    /**
     * 审核商品
     * @param goodsId
     */
    public void updateGoodsCheckStatus(String goodsId, String checkUser, Date checkTime, String checkStatus) {
        goodsMapper.updateGoodsCheckStatus(goodsId,checkUser,checkTime,checkStatus);
    }

    /**
     * 修改商品
     * @param goods
     */
    public void updateGoods(Goods goods) {
        goodsMapper.updateGoods(goods);
    }

    /**
     * 删除商品
     * @param goodsId
     * @return
     */
    @Transactional
    public void deleteGoodsById(String goodsId) {
        goodsMapper.deleteGoodsById(goodsId);
        List<Pic> list=new ArrayList<>();
        list=goodsMapper.getAllPic(goodsId);
        for(Pic pic:list){
            deleteGoodsImage(goodsId,pic.getId(),pic.getPicUrl());
        }
        goodsWeightMapper.deleteGoodsWeightByGoodsId(goodsId);

    }

    /**
     * 查询商品
     * @param goodsId
     * @return
     */
    public Goods getGoodsById(String goodsId) {
        Goods goods=goodsMapper.getGoodsById(goodsId);
        return goods;
    }

    /**
     * 查询商品列表
     * @param param
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public PageModel goodsList(GoodsSearchParam param, int pageIndex, int pageSize) {
        PageModel pm=new PageModel();
        List<Goods> returnList=goodsMapper.getGoodsList(param,getStartRow(pageIndex,pageSize),pageSize);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=goodsMapper.getGoodsListCount(param);
        }
        pm.setCount(count);
        pm.setList(returnList);
        return  pm;
    }

    //删除商品图片
    @Transactional
    public void deleteGoodsImage(String goodsId, String imageId,String imgUrl) {
        //删除数据库信息
        goodsMapper.deleteGoodsImage(goodsId,imageId);
        goodsMapper.deleteImageId(imageId);
        //获取图片数量   为0  修改产品主信息是否存在图片
        int count=goodsMapper.getGoodsImageCount(goodsId);
        if(count==0){
            goodsMapper.updateGoodsIfPic(goodsId,false);
        }
        //删除磁盘图片
        deleteFile(base_img_url+imgUrl);
    }
    //删除商品图片
    @Transactional
    public void deleteGoodsImage(String goodsId, String imageId) {
        //获取数据库图片地址
        Pic pic= goodsMapper.getImageByID(imageId);
        //删除数据库信息
        goodsMapper.deleteGoodsImage(goodsId,imageId);
        goodsMapper.deleteImageId(imageId);
        //获取图片数量   为0  修改产品主信息是否存在图片
        int count=goodsMapper.getGoodsImageCount(goodsId);
        if(count==0){
            goodsMapper.updateGoodsIfPic(goodsId,false);
        }
        //删除磁盘图片
        deleteFile(base_img_url+pic.getPicUrl());
    }

//    /**
//     * 查询商品图片
//     * @param goodsId
//     * @return
//     */
//    public GoodsPic getGoodsPic(String goodsId) {
//        GoodsPic goodsPic=goodsMapper.getGoodsPic(goodsId);
//        return goodsPic;
//    }
    /**
     * @Author: zy
     * @Desc:获取所有图片
     * @Date:  2018/4/3
     * @Param
     */
    public List<Pic> getAllPic(String goodId){
        List<Pic> list=new ArrayList<>();
        list=goodsMapper.getAllPic(goodId);
        return list;
    }
}
